package com.example.springsecurityoauth2.service;

import com.example.springsecurityoauth2.pojo.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class MyUserDetailsService implements UserDetailsService {


    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 密码，这写死，后面是数据库查询
        String encodePassword = passwordEncoder.encode("123456");
        // myUser是UserDetails
        MyUser myUser = new MyUser(username, encodePassword,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin,test,dev"));
        return myUser;
    }
}
